﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Telerik.Web.UI;
using Fly.Common.Linq;

namespace RN.ERP.Web.EnterpriseChange
{
    public partial class EnterpriseChangeList : BasePage
    {
        /// <summary>
        /// 变更类型
        /// </summary>
        public string ObjectType 
        {
            get
            {
                string objectType = "1";
                if (Request["objectType"] != null)
                {
                    objectType = Request["objectType"];
                }
                return objectType;
            }
        }
        /// <summary>
        /// 变更类型
        /// </summary>
        public int ClassID
        {
            get
            {
                int classid = 0;
                if (Request["ClassID"] != null)
                {
                   int.TryParse( Request["ClassID"],out classid);
                }
                return classid;
            }
        }
        /// <summary>
        /// 变更类型
        /// </summary>
        public int RoleID
        {
            get
            {
                int roleid = 0;
                if (Request["ClassID"] != null)
                {
                    int.TryParse(Request["RoleID"], out roleid);
                }
                return roleid;
            }
        }
        BasicService.BasicServiceClient basicClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
                BindRemark();
                BindFlowCode();
                if (UserInfo.RoleType.ToLower() == "user")
                {
                    hdnCorpUserGuid.Value = UserInfo.CorpUserGuid;
                    hdnCorpName.Value = UserInfo.Name;
                }
            }
        }

        protected void ListCorps_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            var strCorpChangeList = basicClient.GetChangeRecords( GetCondition(), ListCorps.CurrentPageIndex + 1, ListCorps.PageSize, UserInfo.CurrentArea);
            var corpChangeList = JsonConvert.DeserializeObject<List<V_ChangeInfo>>(strCorpChangeList);
            var count = basicClient.GetChangeRecordsCount(GetCondition(), UserInfo.CurrentArea);
            ListCorps.VirtualItemCount = count;
            ListCorps.DataSource = corpChangeList;
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorps.MasterTableView.SortExpressions.Clear();
                ListCorps.MasterTableView.GroupByExpressions.Clear();
                ListCorps.Rebind();
            }

        }

        private string GetCondition()
        {
            List<QueryChangeInfoCondition> list = new List<QueryChangeInfoCondition>();
            string flows = ddlStatus.SelectedValue;
            if (!string.IsNullOrEmpty(flows))
            {
                foreach (var f in flows.Split('|'))
                {
                    var newCon = GetCommonQueryChangeInfoCondition();
                    newCon.FlowCode = new ParameterValue<string>() { O = OptEnum.Equal, V = f };
                    if (ClassID != 0)
                    {
                        newCon.RoleID = new ParameterValue<int?>() { O = OptEnum.Equal, V = ClassID };
                    }
                    list.Add(newCon);
                }
            }
            else
            {
                var newCon = GetCommonQueryChangeInfoCondition();
                if (UserInfo.RoleType.ToLower() == "manager")
                {
                    newCon.FlowCode = new ParameterValue<string>() { O=OptEnum.Like,V="019"};
                    if (ClassID != 0)
                    {
                        newCon.RoleID = new ParameterValue<int?>() { O = OptEnum.Equal, V = ClassID };
                    }
                }
                list.Add(newCon);
            }
            return JsonConvert.SerializeObject(list);
        }

        private QueryChangeInfoCondition GetCommonQueryChangeInfoCondition()
        {
            string sysAreaCode = System.Configuration.ConfigurationManager.AppSettings["sysArea"];
            string objectName = txtCorpName.Text.Trim();
            QueryChangeInfoCondition con = new QueryChangeInfoCondition();
            con.ObjectType = new Fly.Common.Linq.ParameterValue<string>() { O = OptEnum.Equal, V = ObjectType };
            if (!string.IsNullOrEmpty(objectName))
            {
                con.ObjectName = new ParameterValue<string>() { O=OptEnum.Like,V=objectName};
            }
            if (RoleID != 0)
                con.RoleID = new ParameterValue<int?>() { O = OptEnum.Equal, V = RoleID };
            con.AreaCode = new ParameterValue<string>() { O = OptEnum.Equal, V = sysAreaCode };
            con.SetAreaCode = new ParameterValue<string>() { O = OptEnum.Like, V = UserInfo.LocalArea };

            ////兼容企业端和主管端
            if (UserInfo.RoleType.ToLower() == "user")
            {
                con.ObjectId = new ParameterValue<string>() { O=OptEnum.Equal,V=UserInfo.CorpUserGuid};
            }
            return con;
        }

        private void InitPage()
        {
            ////兼容企业端和主管端
            if (UserInfo.RoleType.ToLower() == "user")
            {
                UserMenu.Visible = true;
                ManagerMenu.Visible = false;
                ddlStatus.SelectedIndex = 0;
            }
            else
            {
                UserMenu.Visible = false;
                ManagerMenu.Visible = true;
            }
        }

        public string GetFlowCodeTitle(object oflowcode)
        {
            if (oflowcode == null)
            {
                return string.Empty;
            }
            string flowcode = oflowcode.ToString().Trim();
            if (flowcode.StartsWith("1"))
            {
                return "审核通过";
            }
            else if (flowcode.StartsWith("0,019,021"))
            {
                return "主管退回";
            }
            else if (flowcode.IndexOf(",022") > 0)
            {
                return "再次上报";
            }
            else if (flowcode.StartsWith("0,019"))
            {
                return "已上报";
            }
            else if (flowcode.Trim().Equals("0"))
            {
                return "未上报";
            }
            else
            {
                return string.Empty;
            }
        }

        private void BindRemark()
        {
            List<string> list = new List<string>();
            list.Add("企业信息在当前市场审核通过才可以添加变更申请");
            list.Add("变更申请区分市场范围，提交给哪个地区主管，哪个地区主管审核");
            //remark.RemarkList = list;
        }

        private void BindFlowCode()
        {
            string classid = "8020";
            string msg = string.Empty;
            var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(basicClient.GetAllFlowStatus(out msg, UserInfo.CurrentArea, UserInfo.RoleId, classid));
            ddlStatus.Items.Clear();
            foreach (var key in dic.Keys)
            {
                ddlStatus.Items.Add(new ListItem(key, dic[key]));
            }
            ddlStatus.Items.Insert(0, new ListItem("全部", ""));
            if (dic.ContainsKey("处理中"))
            {
                ddlStatus.Items.FindByText("处理中").Selected = true;
            }
        }
    }
}